package com.xmb.wcapi.util;

import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;


public class ConditionUtil {

    public static <T> QueryWrapper<T> convert(String jsonStr, QueryWrapper<T> ew) throws Exception {
        if (StringUtils.isEmpty(jsonStr)) {
            return ew;
        }
        List<Condition> conditionList;
        try {
            conditionList = JSON.parseArray(jsonStr, Condition.class);
        } catch (Exception e) {
            throw new Exception("参数异常");
        }
        List<Condition> firstConditionList = conditionList.stream().filter(condition -> condition.getRowlevel() == 0).collect(Collectors.toList());
        for (Condition condition : firstConditionList) {
            Consumer<QueryWrapper<T>> queryWrapperConsumer = wrapper -> {
                switch (condition.getConditionOptionVal()) {
                    case "equal":
                    	wrapper.eq(condition.getConditionFieldVal(), condition.getConditionValueVal().getValue());
                        break;
                    case "like":
                    	wrapper.like(condition.getConditionFieldVal(), condition.getConditionValueVal().getValue());
                        break;
                    case "between":
                    	wrapper.between(condition.getConditionFieldVal(), condition.getConditionValueLeftVal().getValue(),
                                condition.getConditionValueRightVal().getValue());
                        break;
                    case "unequal":
                    	wrapper.ne(condition.getConditionFieldVal(), condition.getConditionValueVal().getValue());
                        break;
                    case "empty":
                        if (condition.getConditionFieldVal().endsWith("date") || condition.getConditionFieldVal().endsWith("time")) {//
                        	wrapper.isNull(condition.getConditionFieldVal());
                        } else {
                        	wrapper.apply(condition.getConditionFieldVal() + " = '' or " + condition.getConditionFieldVal() + " is null");
                        }
                        break;
                    case "notempty":
                        if (condition.getConditionFieldVal().endsWith("date") || condition.getConditionFieldVal().endsWith("time")) {//
                        	wrapper.isNotNull(condition.getConditionFieldVal());
                        } else {
                        	wrapper.apply(condition.getConditionFieldVal() + " <> '' and " + condition.getConditionFieldVal() + " is not null");
                        }
                        break;
                    case "greater_than":
                    	wrapper.gt(condition.getConditionFieldVal(), condition.getConditionValueVal().getValue());
                        break;
                    case "less_than":
                    	wrapper.lt(condition.getConditionFieldVal(), condition.getConditionValueVal().getValue());
                        break;
                    case "start":
                    	wrapper.likeLeft(condition.getConditionFieldVal(), condition.getConditionValueVal().getValue());
                        break;
                    case "end":
                    	wrapper.likeRight(condition.getConditionFieldVal(), condition.getConditionValueVal().getValue());
                        break;
                }
            };
            //逻辑操作 and or
            String logicalOperator = condition.getLogicalOperator();
            if ("and".equals(logicalOperator)) {
                ew.and(queryWrapperConsumer);
//            	ew = queryWrapperConsumer;
            } else if ("or".equals(logicalOperator)) {
                ew.or(queryWrapperConsumer);
            }
        }
        return ew;
    }
}